package com.destroystokyo.paper.event.entity;

import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityEvent;

public class EntityAttackedEntityEvent extends EntityEvent implements Cancellable {
    private final Entity victim;
    private final DamageCause cause;
    private final double damage;

    public EntityAttackedEntityEvent(Entity attacker, Entity victim, final DamageCause cause, final double damage) {
        super(attacker);
        this.victim = victim;
        this.cause = cause;
        this.damage = damage;
    }

    public Entity getVictim() {
        return victim;
    }

    public DamageCause getCause() {
        return cause;
    }

    public double getDamage() {
        return damage;
    }

    private static final HandlerList handlers = new HandlerList();

    public HandlerList getHandlers() {
        return handlers;
    }

    public static HandlerList getHandlerList() {
        return handlers;
    }

    private boolean cancelled = false;

    @Override
    public boolean isCancelled() {
        return cancelled;
    }

    @Override
    public void setCancelled(boolean cancel) {
        cancelled = cancel;
    }

    public static boolean callEvent(Entity attacker, Entity victim, DamageCause cause, double damage) {
        EntityAttackedEntityEvent event;
        if (attacker instanceof Player) {
            event = new PlayerAttackedEntityEvent((Player) attacker, victim, cause, damage);
        } else if (victim instanceof Player) {
            event = new EntityAttackedPlayerEvent(attacker, (Player) victim, cause, damage);
        } else {
            event = new EntityAttackedEntityEvent(attacker, victim, cause, damage);
        }
        return event.callEvent();
    }
}
